/* eslint-disable no-alert */
import React, { Component } from 'react'
import 'components/dash/main.css'
import { ApolloProvider } from 'react-apollo'
import { Switch, Redirect } from 'react-router'
import { PrivateRoute, AuthedPublicRoute } from 'components/route'
import RoutePaths from 'utils/constents/routePath'
import { client as makeClient } from 'apollo/'
import { HASURA_ADDRESS } from 'env'
import SignUpPage from './plan'
import FetchUser from './fetchUser'
import Home from './home'

export default class Dashboard extends Component {
  constructor(props) {
    super(props)
    const ACCESS_TOKEN = localStorage.getItem('token')
    const client = makeClient.makeClient(HASURA_ADDRESS, ACCESS_TOKEN)
    makeClient.setClient(client)
  }

  render() {
    return (
      <ApolloProvider client={makeClient.getClient()}>
        <Switch>
          <AuthedPublicRoute path={RoutePaths.FETCH_USER} component={FetchUser} exact />
          <AuthedPublicRoute path={RoutePaths.SIGN_UP} component={SignUpPage} exact />
          <PrivateRoute path={RoutePaths.DASHBOARD} component={Home} exact={false} />
          <Redirect exact path="*" to={RoutePaths.NOT_FOUND} />
        </Switch>
      </ApolloProvider>
    )
  }
}
